/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import entity.*;
import java.awt.*;
import java.awt.print.PageFormat;
import java.awt.print.PrinterException;
import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import javax.imageio.ImageIO;

/**
 *
 * @author mender
 */
public class ElevHPEvalDocument extends Document {

    Elev elev;
    Fag fag;
    private Color lineColor;
    private Color headColor;
    private Color fillColor;
    private Color textColor;
    private Font boldFont;
    private Font plainFont;

    public ElevHPEvalDocument(Elev e, Fag f) {
        elev = e;
        fag = f;

        // Colors
        lineColor = new Color(127, 0, 0);
        headColor = new Color(255, 0, 0);
        fillColor = new Color(255, 223, 223);
        textColor = new Color(0, 0, 0);
        // Fonts
        plainFont = new Font(Font.SANS_SERIF, Font.PLAIN, 9);
        boldFont = new Font(Font.SANS_SERIF, Font.BOLD, 9);
    }

    @Override
    public int print(Graphics grphcs, PageFormat pf, int page) throws PrinterException {
        int printResult;

        Graphics2D g = (Graphics2D) grphcs;
        g.setFont(boldFont);
        FontMetrics fm = g.getFontMetrics();

        // Font Height
        int fontHeight = fm.getHeight();
        int portraitWidth = 100;
        int portraitHeight = 150;

        // Diverse mål på papiret.
        double xo = pf.getImageableX();
        double yo = pf.getImageableY();
        double width = pf.getImageableWidth();
        double height = pf.getImageableHeight();
        double center = width / 2.0;
        double middle = height / 2.0;
        g.translate(xo, yo);
        g.setColor(lineColor);

        // Mellemrum mellem linier.
        double spacing = fontHeight * 1.5;

        // Hvor langt nede er vi på papiret?
        int lineY;

        switch (page) {
            case 0:

                // Tegn logo.
                Point p = drawLogo(g, (int) spacing, (int) spacing, (int) width);
                lineY = (int) (spacing + p.y + spacing);

                lineY += spacing;

                String elevNavn = "Elev: " + elev.getFornavn() + " " + elev.getEfternavn();
                String fagString = "Fag: " + fag.getNavn();
                g.drawString(elevNavn, (int) spacing, lineY);
                g.drawString(fagString, (int) (width - spacing - fm.stringWidth(fagString)), lineY);
                lineY += spacing;

                // Tegn handleplaner
                ArrayList<Handleplan> ehp = new ArrayList();
                ehp.addAll(elev.getHandleplans());

                Date latest = null;
                Handleplan latestHp = null;
                for (int j = 0; j < ehp.size(); j++) {
                    if (ehp.get(j).getFag().equals(fag)) {
                        Handleplan hp = ehp.get(j);
                        Date d = hp.getId().getOprettet();
                        if (latest == null) {
                            latest = d;
                            latestHp = hp;
                        } else {
                            if (d.after(latest)) {
                                latest = d;
                                latestHp = hp;
                            }
                        }
                    }
                }
                g.setFont(boldFont);
                g.setColor(lineColor);
                g.drawString("Handleplan", (int) (spacing + fontHeight), lineY + fontHeight);
                lineY += spacing;
                g.drawLine((int) spacing, lineY, (int) (width - spacing), lineY);
                if (latestHp != null) {
                    g.setFont(plainFont);
                    g.setColor(textColor);
                    ArrayList<String> lines = StringUtils.wrapText(latestHp.getBeskrivelse(), (int) (width - spacing * 2), fm);
                    for (int j = 0; j < lines.size(); j++) {
                        g.drawString(lines.get(j), (int) (spacing + fontHeight), lineY + fontHeight);
                        lineY += fontHeight;
                    }
                }

                lineY += spacing;

                // Tegn evaluering
                ArrayList<Evaluering> eev = new ArrayList();
                eev.addAll(elev.getEvaluerings());

                latest = null;
                Evaluering latestEv = null;
                for (int j = 0; j < eev.size(); j++) {
                    if (eev.get(j).getFag().equals(fag)) {
                        Evaluering ev = eev.get(j);
                        Date d = ev.getId().getOprettet();
                        if (latest == null) {
                            latest = d;
                            latestEv = ev;
                        } else {
                            if (d.after(latest)) {
                                latest = d;
                                latestEv = ev;
                            }
                        }
                    }
                }
                g.setFont(boldFont);
                g.setColor(lineColor);
                g.drawString("Evaluering", (int) (spacing + fontHeight), lineY + fontHeight);
                lineY += spacing;
                g.drawLine((int) spacing, lineY, (int) (width - spacing), lineY);
                if (latestEv != null) {
                    g.setFont(plainFont);
                    g.setColor(textColor);
                    ArrayList<String> lines = StringUtils.wrapText(latestEv.getBeskrivelse(), (int) (width - spacing * 2), fm);
                    for (int j = 0; j < lines.size(); j++) {
                        g.drawString(lines.get(j), (int) (spacing + fontHeight), lineY + fontHeight);
                        lineY += fontHeight;
                    }
                }

                lineY += spacing;

                // Tegn potentialer
                ArrayList<Potentiale> ePot = new ArrayList();
                ePot.addAll(elev.getPotentiales());

                g.setFont(boldFont);
                g.setColor(lineColor);
                g.drawString("Potentialer", (int) (spacing + fontHeight), lineY + fontHeight);
                lineY += spacing;
                g.drawLine((int) spacing, lineY, (int) (width - spacing), lineY);
                g.setFont(plainFont);
                g.setColor(textColor);
                for (int i = 0; i < ePot.size(); i++) {
                    Potentiale pot = ePot.get(i);
                    if (!pot.getKompetence() && pot.getTrinbeskrivelse().getTrinoverskrift().getFag().equals(fag)) {
                        ArrayList<String> lines = StringUtils.wrapText(pot.getTrinbeskrivelse().getBeskrivelse(), (int) (width - spacing * 2), fm);
                        for (int j = 0; j < lines.size(); j++) {
                            String s = lines.get(j);
                            if (j == lines.size() - 1) {
                                s += ".";
                            }
                            g.drawString(s, (int) (spacing + fontHeight), lineY + fontHeight);
                            lineY += fontHeight;
                        }
                    }
                }
                lineY += spacing;

                g.setFont(boldFont);
                g.setColor(lineColor);
                g.drawString("Kompetencer", (int) (spacing + fontHeight), lineY + fontHeight);
                lineY += spacing;
                g.drawLine((int) spacing, lineY, (int) (width - spacing), lineY);
                g.setFont(plainFont);
                g.setColor(textColor);
                for (int i = 0; i < ePot.size(); i++) {
                    Potentiale pot = ePot.get(i);
                    if (pot.getKompetence() && pot.getTrinbeskrivelse().getTrinoverskrift().getFag().equals(fag)) {
                        ArrayList<String> lines = StringUtils.wrapText(pot.getTrinbeskrivelse().getBeskrivelse(), (int) (width - spacing * 2), fm);
                        for (int j = 0; j < lines.size(); j++) {
                            String s = lines.get(j);
                            if (j == lines.size() - 1) {
                                s += ".";
                            }
                            g.drawString(s, (int) (spacing + fontHeight), lineY + fontHeight);
                            lineY += fontHeight;
                        }
                    }
                }
                printResult = PAGE_EXISTS;
                break;

            default:
                printResult = NO_SUCH_PAGE;
                break;
        }
        return printResult;
    }

    public Point drawLogo(Graphics2D g, int xPos, int yPos, int pageWidth) {
        g.setFont(boldFont);
        FontMetrics fm = g.getFontMetrics();
        int fontHeight = fm.getHeight();
        double spacing = fontHeight * 1.5;

        String adresse = "Glasmagervej 4, Fensmark";
        String pNum = "4684 Holmegaard";
        String tNum = "Telefon: 55 56 31 12";

        Calendar cal = Calendar.getInstance();
        String dato = "Fensmark, " + cal.get(Calendar.DATE) + ". " + monthName(cal.get(Calendar.MONTH)) + " " + cal.get(Calendar.YEAR);

        int aSize = fm.stringWidth(adresse);
        int pSize = fm.stringWidth(pNum);
        int tSize = fm.stringWidth(tNum);
        int dSize = fm.stringWidth(dato);

        Point p = drawScaledImage(g, "SkoleLogo.png", xPos, yPos, (int) (pageWidth / 2.5), -1);

        g.setColor(textColor);
        g.setFont(plainFont);
        g.drawString(adresse, (int) (pageWidth - spacing - aSize), yPos + fontHeight);
        g.drawString(pNum, (int) (pageWidth - spacing - pSize), (int) (yPos + spacing + fontHeight));
        g.drawString(tNum, (int) (pageWidth - spacing - tSize), (int) (yPos + spacing * 2 + fontHeight));
        g.setFont(boldFont);
        g.drawString(dato, (int) (pageWidth - spacing - dSize), (int) (yPos + spacing * 4 + fontHeight));
        return p;
    }

    public String monthName(int month) {
        String m = "";

        switch (month) {
            case 0:
                m = "Januar";
                break;
            case 1:
                m = "Februar";
                break;
            case 2:
                m = "Marts";
                break;
            case 3:
                m = "April";
                break;
            case 4:
                m = "Maj";
                break;
            case 5:
                m = "Juni";
                break;
            case 6:
                m = "Juli";
                break;
            case 7:
                m = "August";
                break;
            case 8:
                m = "September";
                break;
            case 9:
                m = "Oktober";
                break;
            case 10:
                m = "November";
                break;
            case 11:
                m = "December";
                break;
        }
        return m;
    }

    public void drawImage(Graphics2D g, String fileName, int x, int y) {
        Image img;
        try {
            img = ImageIO.read(new File(fileName));
            g.drawImage(img, x, y, null);
        } catch (Exception ex) {
            // Hvis billedfilen ikke findes eller vi får en null pointer,
            // så lader vi bare være med at tegne billedet.
        }
    }

    public Point drawScaledImage(Graphics2D g, String fileName, int x, int y, int width, int height) {
        Image img;
        Point p;
        try {
            img = ImageIO.read(new File(fileName));
            img = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
            p = new Point(img.getWidth(null), img.getHeight(null));
            g.drawImage(img, x, y, null);
        } catch (Exception ex) {
            // Hvis billedfilen ikke findes eller vi får en null pointer,
            // så lader vi bare være med at tegne billedet.
            p = new Point(0, 0);
        }
        return p;
    }

    @Override
    public String getJobName() {
        return "ELEV: " + elev.getFornavn() + " " + elev.getEfternavn();
    }
}
